// pages/saleOrder/my.js
import util from "../../utils/util";

const network = require("../../utils/network");

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        requestUrl: app.api.saleOrderMy,
        searchForm: {
            part_a: '',
            contract_num: '',
        },
        requestParams: {},
        plan: {},
        dialogShow: false,
        tmpSaleId: -1,
        tmpType: 0,
        fabs: [{
            iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145321081195.png',
            text: '搜索'
        }, {
            iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145517219958.png',
            text: '刷新'
        }],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        // this.selectComponent('.sale-list-component').handlePullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.selectComponent('.sale-list-component').handleReachBottom()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    toPage_left1({detail: {id}}) {
        this.setData({
            tmpSaleId: id
        })
        network.reqPromise("GET", {
            uri: app.api.planListById,
            params: {
                related_id: id,
                related_type: 2,
            }
        }).then(res => {
            if (res.data.length === 1) {
                this.setData({
                    plan: res.data[0],
                    dialogShow: true
                })
            } else {
                util.showToast('暂无变更类型的锁位')
            }
        }).catch(() => {
            util.showToast('获取关联的锁位失败')
        })
    },

    toPage_left2({detail: {id}}) {
        wx.navigateTo({
            url: '/pages/saleOrder/detail?id=' + id
        });
    },

    // todo
    inputChange(e) {
        this.setData({
            [e.target.dataset.key]: e.detail
        })
    },

    showSearchBox() {
        this.selectComponent('.search-box').show()
    },
    onClose() {
        this.setData({
            dialogShow: false
        })
    },
    onConfirm() {
        wx.navigateTo({
            url: '/pages/saleOrder/detail?is_create=' + true + '&is_update=' + true + '&plan_id=' + this.data.plan.id + '&customer_id=' + this.data.plan.before_update_sale_customer_id
                + '&contract_id=' + this.data.plan.before_update_sale_contract_id + '&source_sale_id=' + this.data.tmpSaleId
        });
    },
    resetSearch() {
        this.setData({
            searchForm: {
                part_a: '',
                contract_num: '',
            }
        });
    },
    clickFab({
                 detail
             }) {
        const _this = this
        if (detail === '刷新') {
            _this.resetSearch();
            _this.setData({
                searchForm: Object.assign({}, _this.data.defaultRequestParams)
            }, function () {
                _this.selectComponent('.sale-list-component').handlePullDownRefresh()
            })
        }
        if (detail === '搜索') {
            _this.selectComponent('.search-box').show();
        }
    },
    onSearch() {
        const _this = this;
        const _params = JSON.parse(JSON.stringify(_this.data.searchForm || {}));
        let params = {};
        if (!!_params) {
            const keys = Object.keys(_params) || [];
            keys.forEach(key => {
                if (_params[key] != null && _params[key] != undefined) params[key] = _params[key];
            });
        }
        _this.setData({
            requestParams: Object.assign({}, _this.data.searchForm, params)
        }, function () {
            _this.selectComponent('.sale-list-component').handlePullDownRefresh()
            _this.selectComponent('.search-box').close();
        })
    },
    onReset() {
        const _this = this;
        _this.resetSearch();
        _this.setData({
            requestParams: Object.assign({}, _this.data.searchForm)
        }, function () {
            _this.selectComponent('.sale-list-component').handlePullDownRefresh()
            _this.selectComponent('.search-box').close();
        })
    },
})
